package com.xiaoshuidi.cloud.module.contract.service.contract.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.module.contract.controller.admin.contract.vo.ContractChangingRoomsRecordPageReq;
import com.xiaoshuidi.cloud.module.contract.controller.admin.contract.vo.ContractChangingRoomsRecordPageResp;
import com.xiaoshuidi.cloud.module.contract.convert.contract.ContractChangingRoomsRecordConvert;
import com.xiaoshuidi.cloud.module.contract.dal.dataobject.contract.ContractChangingRoomsRecordDo;
import com.xiaoshuidi.cloud.module.contract.dal.dataobject.contract.ContractRoomDO;
import com.xiaoshuidi.cloud.module.contract.dal.mysql.contract.ContractChangingRoomsRecordMapper;
import com.xiaoshuidi.cloud.module.contract.enums.contract.HydropowerShareMethodEnum;
import com.xiaoshuidi.cloud.module.contract.service.contract.ContractChangingRoomsRecordService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author Feng
 * @date 2023/7/3
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class ContractChangingRoomsRecordServiceImpl extends ServiceImpl<ContractChangingRoomsRecordMapper, ContractChangingRoomsRecordDo>
        implements ContractChangingRoomsRecordService {
    @Override
    public PageResult<ContractChangingRoomsRecordPageResp> page(ContractChangingRoomsRecordPageReq req) {
        Page<ContractChangingRoomsRecordDo> page = Page.of(req.getPageNo(), req.getPageSize());
        LambdaQueryWrapper<ContractChangingRoomsRecordDo> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ContractChangingRoomsRecordDo::getCustomerCompanyId, req.getCustomerCompanyId())
                .and(StrUtil.isNotEmpty(req.getRoomName()), s -> s.like(ContractChangingRoomsRecordDo::getSourceRoomName, req.getRoomName())
                        .or().like(ContractChangingRoomsRecordDo::getTargetRoomName, req.getRoomName()));
        this.page(page, wrapper);
        List<ContractChangingRoomsRecordDo> records = page.getRecords();
        if (CollectionUtil.isNotEmpty(records)) {
            List<ContractChangingRoomsRecordPageResp> resps = records.stream().map(s -> {
                ContractChangingRoomsRecordPageResp resp = ContractChangingRoomsRecordConvert.INSTANCE.convert(s);
                resp.setSourceHydropowerShareMethodStr(HydropowerShareMethodEnum.getMsgByCode(s.getSourceHydropowerShareMethod()));
                resp.setTargetHydropowerShareMethodStr(HydropowerShareMethodEnum.getMsgByCode(s.getTargetHydropowerShareMethod()));
                return resp;
            }).collect(Collectors.toList());
            PageResult<ContractChangingRoomsRecordPageResp> result = new PageResult<>(resps, page.getTotal());
            return result;
        }
        return new PageResult<>();
    }

    @Override
    public void insert(ContractRoomDO contractRoomDO, ContractRoomDO contractRoom) {

        ContractChangingRoomsRecordDo recordDo=new ContractChangingRoomsRecordDo();
        recordDo.setSourceApartmentId(contractRoom.getApartmentId());
        recordDo.setSourceApartmentName(contractRoom.getApartmentName());
        recordDo.setSourceRoomId(contractRoom.getRoomId());
        recordDo.setSourceRoomName(contractRoom.getRoomName());
        recordDo.setSourceContractId(contractRoom.getId());
        recordDo.setSourceMonthRent(contractRoom.getMonthRent());
        recordDo.setSourceHydropowerShareMethod(contractRoom.getOrderShareMethod());
        recordDo.setTargetApartmentId(contractRoomDO.getApartmentId());
        recordDo.setTargetApartmentName(contractRoomDO.getApartmentName());
        recordDo.setTargetRoomId(contractRoomDO.getRoomId());
        recordDo.setTargetRoomName(contractRoomDO.getRoomName());
        recordDo.setTargetContractId(contractRoomDO.getId());
        recordDo.setTargetMonthRent(contractRoomDO.getMonthRent());
        recordDo.setTargetHydropowerShareMethod(contractRoomDO.getOrderShareMethod());
        recordDo.setChangeDate(contractRoomDO.getStartTime());
        recordDo.setCustomerCompanyId(contractRoomDO.getCustomerCompanyId());
        recordDo.setMemberId(contractRoomDO.getMemberId());

        this.save(recordDo);
    }
}
